﻿using AutoMapper;
using MediatR;
using SmartProduction.Domain.客户管理;
using SmartProduction.ErrorCode;
using SmartProduction.Infarstructrue.Interface;
using SmartProduction.Read.API.Application.Commands.Export;
using SmartProduction.Read.API.Dtos;
using System.Reflection.Emit;

namespace SmartProduction.Read.API.Application.CommandHandlers.ExportHandler
{
    /// <summary>
    /// 客户查询命令处理
    /// </summary>
    public class ExportClientCommandHandler : IRequestHandler<ExportClientCommand, APIResult<List<ExportClientCommandityDTO>>>
    {
        /// <summary>
        /// 客户仓储
        /// </summary>
        private readonly IBaseRepository<ClientInfo> clientRepository;

        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="clientRepository">客户仓储</param>
        /// <param name="mapper">映射</param>
        public ExportClientCommandHandler(IBaseRepository<ClientInfo> clientRepository,IMapper mapper)
        {
            this.clientRepository = clientRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>
        public Task<APIResult<List<ExportClientCommandityDTO>>> Handle(ExportClientCommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIResult<List<ExportClientCommandityDTO>> result = new APIResult<List<ExportClientCommandityDTO>>();

                var list = from a in clientRepository.GetAll().Result
                           where request.ClientInfoId.Contains(a.ClientInfoId)
                           select new ExportClientCommandityDTO
                           {
                               ClientInfoNumer = a.ClientInfoNumer,
                               ClientInfoAddr = a.ClientInfoAddr,
                               ClientInfoName = a.ClientInfoName,
                               ClientInfoType = a.ClientInfoType,
                               ClientInfoEmail = a.ClientInfoEmail,
                               ClientInfoPhone = a.ClientInfoPhone,
                               CountryCity = a.CountryCity,
                               CountryName = a.CountryName,
                           };

                result.Code = APIEnums.查询成功;
                result.Msg = APIEnums.查询成功.ToString();
                result.data = list.ToList();

                return Task.FromResult(result);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
